package customer.zspt.cn.customer.Widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.bean.OrderMsgDao;

/**
 * Created by Administrator on 2018/7/12 0012.
 */

public class ViewPagerIndicator extends LinearLayout implements ViewPager.OnPageChangeListener{
    private final int width;
    public ViewPager vp;
    public int index;
    public  HorizontalScrollView Hzview;
    public ViewPagerIndicator(Context context) {
        this(context,null);
    }
    public ViewPagerIndicator(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    public ViewPagerIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
         width = wm.getDefaultDisplay().getWidth();
    }


    public void setChildATitle(String[] title, List<List<OrderMsgDao.OrderMsg>> all)
    {
        for (int i=0;i<title.length;i++)
        {
            final Item item = new Item(getContext());
            item.setTitle(title[i]);
            item.setIndex(i);
            if(i==0)
            {
                item.setColor(getResources().getColor(R.color.green));
                item.setLineState(View.VISIBLE);
            }
            item.setCount(all.get(i).size());
            item.setOnClickListener(mLister);
            this.addView(item);
        }
    }
    private OnClickListener mLister = new OnClickListener() {
        @Override
        public void onClick(View view) {
             Item item = (Item) view;
            setState(item.getIndex());
            vp.setCurrentItem(item.getIndex());
        }
    };
    public void setVp(ViewPager vp,HorizontalScrollView Hzview)
    {
        this.Hzview = Hzview;
        this.vp = vp;
        if(vp.getAdapter()!=null)
        {
            vp.setOnPageChangeListener(this);
        }
        else{
            throw new IllegalStateException("VP not Adapter");
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }
    private void setState(int position)
    {
        Item stateSelected = (Item) this.getChildAt(position);
        if(stateSelected.getLeft()>width/2)
        {
            Hzview.smoothScrollTo(stateSelected.getLeft()+(stateSelected.getViewWidth(stateSelected)/2)-width/2,0);
        }
        else{
            Hzview.smoothScrollTo(0,0);
        }
        for(int i=0;i<this.getChildCount();i++)
        {
            Item item = (Item) this.getChildAt(i);
            item.setLineState(position==item.getIndex()?View.VISIBLE:View.INVISIBLE);
            item.setColor(position==item.getIndex()?getResources().getColor(R.color.green):getResources().getColor(R.color.textColor));
        }
    }
    @Override
    public void onPageSelected(int position) {
        setState(position);
    }
    @Override
    public void onPageScrollStateChanged(int state) {

    }
    public class Item extends LinearLayout{

        private  View view;
        @ViewInject(R.id.msg_title)
        private TextView title_tv;
        @ViewInject(R.id.msg_count)
        private TextView msg_count_tv;

        @ViewInject(R.id.line)
        private View line;
        private int index = 0;
        public Item(Context context) {
            this(context,null);
        }
        public Item(Context context, @Nullable AttributeSet attrs) {
            this(context, null,0);
        }

        public Item(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            view = View.inflate(context, R.layout.order_msg_titleitem,this);
            ViewUtils.inject(this,view);
            line.setVisibility(INVISIBLE);
        }
        public void setLineState(int state){
            line.setVisibility(state);
        }
        //宽
        public int getViewWidth(Item view){
            view.measure(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            return view.getMeasuredWidth();
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
        public void setColor(int res)
        {
            title_tv.setTextColor(res);
        }
        public void setTitle(String title){
            title_tv.setText(title);
        }
        public void setIndex(int index)
        {
            this.index = index;
        }

        public int getIndex()
        {
            return index;
        }

        public void setCount(int count)
        {
            if(count>99)
            {
                msg_count_tv.setText("99+");
            }
            else{
                msg_count_tv.setText(count+"");
            }
        }
    }
}
